package com.uniapp.server.service.impl;

import com.uniapp.server.model.RecentlyEntity;
import com.uniapp.server.repository.RecentlyRepository;
import com.uniapp.server.service.RecentlyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

/**
 * @author cyt
 * @describe
 * @create 2021-01-15 17:33
 */
@Service
public class RecentlyServiceImpl implements RecentlyService {
    @Autowired
    private RecentlyRepository recentlyRepository;


    @Override
    public void add(RecentlyEntity entity) {
        recentlyRepository.save(entity);
    }

    @Override
    public void delete(Integer id) {
        recentlyRepository.deleteById(id);
    }

    @Override
    public void update(RecentlyEntity recentlyEntity) {
        recentlyRepository.save(recentlyEntity);
    }

    @Override
    public RecentlyEntity getById(Integer id) {
        return recentlyRepository.findById(id).get();
    }

    @Override
    public Page<RecentlyEntity> getPage(Integer page, Integer size) {
        return recentlyRepository.findAll(getPageable(page, size));
    }

    static Pageable getPageable(Integer page, Integer size) {
        return PageRequest.of(
                page - 1, size,
                Sort.by(Sort.Direction.ASC, "id")
        );
    }
}
